home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_09 / saks / strtst4.cpp < prev   
Encoding:
C/C++ Source or Header  |  1994-07-12  |  1.2 KB  |  78 lines

  1. Listing 5 - A test program for a queue of str implemented using genq
  2.  
  3. //
  4. // strtst4.cpp - test genq4 using str elements
  5. //
  6.  
  7. #include <iostream.h>
  8.  
  9. #include "showheap.h"
  10. #include "strq4.h"
  11.  
  12. void print_str(void *e, void *args)
  13.     {
  14.     *(ostream *)args << ' ' << *(str *)e;
  15.     }
  16.  
  17. inline ostream &operator<<(ostream &os, strq &q)
  18.     {
  19.     q.apply(print_str, &os);
  20.     return os;
  21.     }
  22.  
  23. #define DIM(a) (sizeof(a)/sizeof(a[0]))
  24.  
  25. void test()
  26.     {
  27.     char c;
  28.     size_t qn;
  29.     str qe;
  30.     strq q[4];
  31.     while (cin >> c)
  32.         {
  33.         showheap();
  34.         if (c == 'q')
  35.             break;
  36.         if (c == 'a')
  37.             {
  38.             cin >> qn >> qe;
  39.             if (qn >= DIM(q))
  40.                 cout << "no such queue\n";
  41.             else
  42.                 q[qn].append(qe);
  43.             }
  44.         else if (c == 'c')
  45.             {
  46.             cin >> qn;
  47.             if (qn >= DIM(q))
  48.                 cout << "no such queue\n";
  49.             else
  50.                 q[qn].clear();
  51.             }
  52.         else if (c == 'r')
  53.             {
  54.             cin >> qn;
  55.             if (qn >= DIM(q))
  56.                 cout << "no such queue\n";
  57.             else if (q[qn].remove(qe))
  58.                 cout << "removed " << qe << '\n';
  59.             else
  60.                 cout << "q[" << qn << "] is empty\n";
  61.             }
  62.         else
  63.             continue;
  64.         for (size_t i = 0; i < DIM(q); ++i)
  65.             cout << i << ':' << q[i] << '\n';
  66.         }
  67.     }
  68.  
  69. int main()
  70.     {
  71.     showheap();
  72.     test();
  73.     showheap();
  74.     return 0;
  75.     }
  76.  
  77.  
  78.